package com.guogee.pdb.dao;

import java.sql.Date;

import com.guogee.pdb.bean.DoorbellInfo;
import com.guogee.pdb.db.DBHelper.AlarmRecordCollection;
import com.guogee.pdb.db.DBHelper.DoorBellInfoCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class DoorbellInfoDAO extends ADao<DoorbellInfo> {

	public DoorbellInfoDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(DoorBellInfoCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(DoorbellInfo obj) {
		
		if(insertItem1(obj) != null){
			return true;
		}else{
			return false;
		}
	
	}

	@Override
	public DoorbellInfo parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
		
	}

	@Override
	public String insertItem1(DoorbellInfo obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(DoorBellInfoCollection.DEVICE_TYPE, obj.getDeviceType());
		dbObj.put(DoorBellInfoCollection.MAC, obj.getMac());
		dbObj.put(DoorBellInfoCollection.FUN, obj.getFun());
		dbObj.put(DoorBellInfoCollection.PATH, obj.getPath());
		dbObj.put(DoorBellInfoCollection.TIME, obj.getTime());
		dbObj.put(DoorBellInfoCollection.DATA, obj.getData() == null ? "" : obj.getData());
		dbObj.put(AlarmRecordCollection.INVISIBLE, obj.getInvisible());
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(DoorBellInfoCollection.ID).toString();
		}else{
			return null;
		}
	}

	@Override
	public DoorbellInfo parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		DoorbellInfo doorbell = new DoorbellInfo();
		doorbell.setId(object.get(DoorBellInfoCollection.ID).toString());
		doorbell.setDeviceType(Integer.valueOf(object.get(DoorBellInfoCollection.DEVICE_TYPE).toString()));
		doorbell.setMac(object.get(DoorBellInfoCollection.MAC).toString());
		doorbell.setFun(Integer.valueOf(object.get(DoorBellInfoCollection.FUN).toString()));
		doorbell.setPath(object.get(DoorBellInfoCollection.PATH).toString());
		doorbell.setTime(Long.valueOf(object.get(DoorBellInfoCollection.TIME).toString()));
		doorbell.setData(object.get(DoorBellInfoCollection.DATA).toString());
		
		if (object.get(AlarmRecordCollection.INVISIBLE) != null) {
			doorbell.setInvisible(Byte.parseByte(object.get(AlarmRecordCollection.INVISIBLE).toString()));
		} 
		return doorbell;
	}
}
